package io.renren.modules.web.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.web.entity.CategoryEntity;
import io.renren.modules.web.service.CategoryService;
import io.renren.modules.web.vo.CategoryLayerDetailVo;
import io.renren.modules.web.vo.CategoryLayerVo;
import io.renren.modules.web.vo.CategoryVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 分类
 *
 * @author chensixiang
 * @email chensixiang1234@gmail.com
 * @date 2021-08-29 16:56:05
 */
@RestController
@RequestMapping("web/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/get/category/list")
    @RequiresPermissions("web:category:get:category:list")
    public R categoryList() {
        List<CategoryEntity> categoryEntities = categoryService.getCategoryList();
        return R.ok().put("data", categoryEntities);
    }

    @GetMapping("/get/category/{id}/layer")
    public R getCategoryLayer(@PathVariable Long id) {
        CategoryLayerVo categoryLayerVo = categoryService.getCategoryLayer(id);
        return R.ok().put("data", categoryLayerVo);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("web:category:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("web:category:info")
    public R info(@PathVariable("id") Long id) {
        CategoryEntity category = categoryService.getById(id);

        return R.ok().put("category", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("web:category:save")
    public R save(@RequestBody CategoryEntity category) {
        categoryService.save(category);

        return R.ok();
    }

    @RequestMapping("/saveBatch")
    @RequiresPermissions("web:category:saveBatch")
    public R saveBatch(@RequestBody CategoryVo categoryVo) {
        categoryService.updateBatchById(categoryVo.getCategoryList());
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("web:category:update")
    public R update(@RequestBody CategoryEntity category) {
        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("web:category:delete")
    public R delete(@RequestBody Long[] ids) {
        categoryService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
